package com.ruoyi.web.controller.epm;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.EpmInformation;
import com.ruoyi.system.domain.EpmInformationResignationRecord;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/Empment/employedoperate")
public class EpmEmployedOperateController extends BaseController {

    @Autowired
    private IEpmEmployedOperateService iEpmEmployedOperateService;

    @Autowired
    private IEpmInformationHistoryService epmInformationHistoryService;
    @Autowired
    private IEpmInformationService epmInformationService;



    @Autowired
    private IEpmInformationResignationRecordService epmInformationResignationRecordService;
    /**
    * 查询在职员工列表
    */
    @PreAuthorize("@ss.hasPermi('Empment:employedoperate:update')")
    @GetMapping(value = "/operatelist")
    public TableDataInfo operatelist(EpmInformation epmInformation) {
        startPage();
        List<EpmInformation> epmList = iEpmEmployedOperateService.operatelist(epmInformation);
        return getDataTable(epmList);
    }

    /**
     * 修改在职员工为离职员工
     * */
    @PreAuthorize("@ss.hasPermi('Empment:employedoperate:update')")
    @PostMapping("/updateoperate")
    public AjaxResult updateoperate(@RequestBody EpmInformationResignationRecord epmInformationResignationRecord) {

        EpmInformation epm=epmInformationService.selectEpmInformationById(epmInformationResignationRecord.getUserId());

        //记录离职记录
        //System.out.println( epmInformationResignationRecordService.useEpm_updateEpmInformationResignationRecord(epmInformationResignationRecord,epm));
        epmInformationResignationRecordService.insertEpmInformationResignationRecord(epmInformationResignationRecordService.useEpm_updateEpmInformationResignationRecord(epmInformationResignationRecord,epm));
        long userIdLong =epmInformationResignationRecord.getUserId();
        List<Integer> userIds = new ArrayList<>();
        // 将 long 转换为 int 并添加到 List 中
        userIds.add((int) userIdLong); // 强制转换
        //修改员工在职状态
        iEpmEmployedOperateService.updateoperate(userIds);
        epmInformationHistoryService.insert_epm_out(userIds,getUsername());

        return AjaxResult.success();
    }
}
